package org.essilab.analyzer.analyzer;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import org.essilab.analyzer.Configuration.Config;
import org.essilab.analyzer.Configuration.FileTerm;

/**
 * This class is used to create the object that will be saved to a file from a
 * given Term.
 *
 * @author Mohamed-Amine Chouchene, Ahcène Idinarere, Vincent Nock, Alain
 * Janin-Manificat
 */
public class Files {

    private String pathFolder = "";
    private String mainFolder = "terms/";
    private String path = "";

    /**
     * Files Constructor. Init Files#pathFolder and Files#mainFolder.
     *
     * @see Config#get(java.lang.String)
     */
    public Files() {
        pathFolder = Config.get("savePath");
        path = pathFolder + mainFolder;
        File f = new File(path);
        if (!f.exists()) {
            f.mkdirs();
        }
    }

    
    /**
     * Return the absolute path of fileName.
     *
     * @param fileName String - Name of the file.
     * @return String - Return the absolute path of fileName.
     */
    public String getFilePathFromTitle(String fileName) {
        String subName = "";
        if (4 < fileName.length()) {
            subName = fileName.substring(0, 4);
        } else if (3 < fileName.length()) {
            subName = fileName.substring(0, 3);
        } else if (2 < fileName.length()) {
            subName = fileName.substring(0, 2);
        } else {
            subName = fileName;
        }
        String name = path + subName + "/" + fileName;
        return name;
    }

    /**
     * Save a Term in a file.
     *
     * @param term Term - the term to save in a file.
     */
    public void saveTerm(Term term) {
        String fileName = term.getTitle().trim();
        if (fileName.contains("/")) {
            fileName = fileName.replace("/", "_").trim();
        }
        String subName = "";
        if (4 < fileName.length()) {
            subName = fileName.substring(0, 4).trim();
        } else if (3 < fileName.length()) {
            subName = fileName.substring(0, 3).trim();
        } else if (2 < fileName.length()) {
            subName = fileName.substring(0, 2).trim();
        } else {
            subName = fileName.trim();
        }
        String name = path +subName + "/";
        File f = new File(name);
        if (!f.exists())
        	f.mkdirs();
        f = new File(name + fileName);
        if (f.exists()) {
            int i = 0;
            while (f.exists()) {
                f = new File(name + fileName + "_" + i);
                i++;
            }
        }
         try {
            FileTerm ft = new FileTerm(f, true);
            prepareOjectToSave(ft, term);
            ft.saveFile();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Fill the FileTerm from the Term that will be saved.
     *
     * @param ft FileTerm - the object used to save a Term.
     * @param term Term - the term used to fill ft.
     */
    private void prepareOjectToSave(FileTerm ft, Term term) {
        Map<String, String> info = new HashMap<>();
        if (null != term.getTitle() && !term.getTitle().trim().isEmpty())
            info.put("title", term.getTitle().trim());
        /*
         * if(null!=term.getNormalizedTitle() &&
         * !term.getNormalizedTitle().trim().isEmpty())
         * info.put("normalized_title",term.getNormalizedTitle().trim());
         */
        if (null != term.getCreatedAt() && !term.getCreatedAt().trim().isEmpty())
            info.put("created_at", term.getCreatedAt().trim());
        
        if (null != term.getLang() && !term.getLang().trim().isEmpty())
            info.put("lang", term.getLang().trim());
        
        if (0 < info.size()) 
            ft.putProperty("info", info);
        
        if (null != term.getAbbreviations() && 0 < term.getAbbreviations().size()) 
            ft.putProperty("abbreviations", term.getAbbreviations());
        
        if (null != term.getAlmostSynonymous() && 0 < term.getAlmostSynonymous().size()) 
            ft.putProperty("almost_synonymous", term.getAlmostSynonymous());
        
        if (null != term.getAntonym() && 0 < term.getAntonym().size()) 
            ft.putProperty("antonyms", term.getAntonym());
        
        if (null != term.getDerives() && 0 < term.getDerives().size()) 
            ft.putProperty("derives", term.getDerives());
        
        if (null != term.getDimintives() && 0 < term.getDimintives().size()) 
            ft.putProperty("dimintives", term.getDimintives());
        
        if (null != term.getError() && 0 < term.getError().size()) 
            ft.putProperty("errors", term.getError());
        
        if (null != term.getfAdj() && 0 < term.getfAdj().size()) 
            ft.putProperty("form_adjectif", term.getfAdj());
        
        if (null != term.getfAdjIndef() && 0 < term.getfAdjIndef().size()) 
            ft.putProperty("form_adjectif_undefine", term.getfAdjIndef());
        
        if (null != term.getfInterj() && 0 < term.getfInterj().size()) 
            ft.putProperty("form_interjectif", term.getfInterj());
        
        if (null != term.getfLAdv() && 0 < term.getfLAdv().size()) 
            ft.putProperty("form_locution_adverb", term.getfLAdv());
        
        if (null != term.getfLNoun() && 0 < term.getfLNoun().size()) 
            ft.putProperty("form__locution_noun", term.getfLNoun());
        
        if (null != term.getfLVerb() && 0 < term.getfLVerb().size()) 
            ft.putProperty("form_locution_verb", term.getfLVerb());
        
        if (null != term.getfNoun() && 0 < term.getfNoun().size()) 
            ft.putProperty("form_noun", term.getfNoun());
        
        if (null != term.getfVerb() && 0 < term.getfVerb().size()) 
            ft.putProperty("form_verb", term.getfVerb());
        
        if (null != term.getHolonyms() && 0 < term.getHolonyms().size()) 
            ft.putProperty("holonyms", term.getHolonyms());
        
        if (null != term.getHyperonyms() && 0 < term.getHyperonyms().size()) 
            ft.putProperty("hyperonyms", term.getHyperonyms());
        
        if (null != term.getHyponyms() && 0 < term.getHyponyms().size()) 
            ft.putProperty("hyponyms", term.getHyponyms());
        
        if (null != term.getMeronymes() && 0 < term.getMeronymes().size()) 
            ft.putProperty("meronyms", term.getMeronymes());
        
        if (null != term.getRelated() && 0 < term.getRelated().size()) 
            ft.putProperty("related", term.getRelated());
        
        if (null != term.getSynonymous() && 0 < term.getSynonymous().size())
            ft.putProperty("synonymous", term.getSynonymous());
        
        if (null != term.getTranslations() && 0 < term.getTranslations().size()) {
            ft.putProperty("translations", term.getTranslations());
        }
        if (null != term.getTroponyms() && 0 < term.getTroponyms().size()) {
            ft.putProperty("troponyms", term.getTroponyms());
        }
        if (null != term.getTypes() && 0 < term.getTypes().size()) {
            ft.putProperty("types", term.getTypes());
        }
        if (null != term.getTypographicalVariants() && 0 < term.getTypographicalVariants().size()) {
            ft.putProperty("typographical_variants", term.getTypographicalVariants());
        }
        if (null != term.getVariantOrthoraphic() && 0 < term.getVariantOrthoraphic().size()) {
            ft.putProperty("variant_orthoraphic", term.getVariantOrthoraphic());
        }
        if (null != term.getVariants() && 0 < term.getVariants().size()) {
            ft.putProperty("variants", term.getVariants());
        }
        if (null != term.getVocabulary() && 0 < term.getVocabulary().size()) {
            ft.putProperty("vocabulary", term.getVocabulary());
        }
    }
}